<template>
<div class="admin-add">
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="点击上传头像">
    </el-form-item>
    <el-form-item label="" prop="url">
      <el-upload
          :class="{uploadSty:showBtnDealImg,disUploadSty:noneBtnImg}"
          ref="upload"
          action="http://localhost:9600/upload/file"
          :headers="headers"
          limit="1"
          :on-success="handle_success"
          list-type="picture-card"
          :auto-upload="false"
          :on-change="change"
      >
        <div style="left: -20px;top: -18px;position: absolute">
          <i class="el-icon-plus avatar-uploader-icon"></i>
        </div>
      </el-upload>

    </el-form-item>
    <el-form-item label="姓名" prop="netName">
      <el-input v-model="ruleForm.netName" style="width: 25%"></el-input>
    </el-form-item>
    <el-form-item label="账号" prop="netAccount">
      <el-input v-model="ruleForm.netAccount" style="width: 25%"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="netPassword">
      <el-input type="password" v-model="ruleForm.netPassword" style="width: 25%"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="netPhone">
      <el-input v-model="ruleForm.netPhone" style="width: 25%"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
import {addAdmin} from "@/api/admin_api";

export default {
  name: "admin_add",
  inject:['reload'],
  data() {
    return {
      ruleForm: {
        netName: '',
        netPassword: '',
        netAccount: '',
        netPhone:'',
        netIcon:''
      },

      rules: {
        netAccount: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        netPassword:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        netPhone:[
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '长度必须为11个字符', trigger: 'blur' }
        ],
      },

      fileList:'',
      file:'',
      showBtnDealImg:true,
      noneBtnImg:false,
      testFile:'',

    };
  },
  methods: {


    // 状态改变方法
    change(file,fileList){
      this.$refs.upload.submit();
      console.log("number ==== "+file);
        this.showBtnDealImg = false;
        this.noneBtnImg = true;

    },


    handle_success(response){
      console.log("获取图片")
        this.ruleForm.netIcon = response.data;
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        console.log("name=="+this.ruleForm.netName)
        console.log("netAccount=="+this.ruleForm.netAccount)
        if (valid) {
          addAdmin(this.ruleForm).then(()=>{
            this.$message({
              message: '添加成功！',
              type: 'success'
            });
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.reload();
      this.showBtnDealImg = true;
      this.noneBtnImg = false;
    },

  }
}
</script>

<style>
.admin-add .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.admin-add .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.admin-add .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}

/*上传文件*/
.admin-add .uploadSty .el-upload--picture-card{
  width:110px;
  height:110px;
  line-height:110px;
}
.admin-add .disUploadSty .el-upload--picture-card{
  display:none;   /* 上传按钮隐藏 */
}
</style>